<template>
    <div>
        <el-dialog
                title="添加新员工"
                :visible.sync="saveNewEmployeeShow"
                :show-close="false"
                :before-close="handleClose">
            <el-form class="empData" :model="EmployeeForm" :rules="rules" ref="ruleForm">
                <el-form-item label="员工名称" prop="name">
                    <el-input v-model.trim="EmployeeForm.name" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="员工手机号" prop="phone">
                    <el-input v-model.trim="EmployeeForm.phone" minlength="11" maxlength="11" placeholder="请输入手机号"/>
                </el-form-item>
                <el-form-item label="工作种类选择">
                    <el-select v-model="EmployeeForm.type" placeholder="请选择您的私教">
                        <el-option v-for="item in empTypes" :value="item"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
         <el-button type="primary" @click="submit" plain>确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {saveNewEmployee} from '@/utils/apis/employee'

    export default {
        name: "NewEmployee",
        data() {
            let checkPhone = (rule, value, callback) => {
                // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
                if (value === "") {
                    callback(new Error("请输入手机号"));
                } else if (!this.isCellPhone(value)) {
                    // 引入methods中封装的检查手机格式的方法
                    callback(new Error("请输入正确的手机号!"));
                } else {
                    callback();
                }
            };
            return {
                EmployeeForm: {
                    name: '',
                    phone: '',
                    type: '教练'
                },
                empTypes: [
                    '教练',
                    '前台',
                    '检修员',
                    '会计',
                    '保洁'
                ],
                rules: {
                    name: [
                        {required: true, message: '请输入员工名称', trigger: 'blur'},
                        {min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    phone: [{required: true, validator: checkPhone, trigger: 'blur'}]
                }
            }
        },
        props: ['saveNewEmployeeShow'],
        methods: {
            //关闭弹窗
            handleClose() {
                this.cancel()
            },
            cancel() {
                this.$emit('cancel')
                this.$notify.info('取消添加')
            },
            submit() {
                if (this.EmployeeForm.name === '' || this.EmployeeForm.phone.length !== 11) {
                    this.$message.warning('请正确填写信息')
                } else {
                    saveNewEmployee(this.EmployeeForm).then(res => {
                        if (res.data.code === 200) {
                            this.$notify.success('添加成功')
                            this.EmployeeForm.name = ''
                            this.EmployeeForm.phone = ''
                            this.EmployeeForm.type = '教练'
                            this.$emit('cancel')
                        } else {
                            this.$message.error(res.data.message)
                        }
                    })
                }
            },
            //判断是否为手机号
            isCellPhone(val) {
                if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) {
                    return false;
                } else {
                    return true;
                }
            },
        }
    }
</script>

<style scoped>
    .empData {
        background-color: rgba(246, 246, 246, 0.21);

    }
</style>